package com.wcl.nullmusic.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

import com.wcl.nullmusic.utils.TimeParseTool;

public class LyricView2 extends TextView {
    private Paint mPaint;
    private float mX;
    private Paint mPathPaint;
    public int index = 0;
    public float mTouchHistoryY;
    private int mY;
    private int currentDuringTime;//��ǰ��ʳ���ʱ��
    private float middleY;//Y���м�
    private final int DY = 40;//ÿһ�еļ��
    public float driftx;//xƫ����
    public float drifty;//yƫ����
    private float drift_r;
    public boolean showprogress;//����ʱ��ʾ���
    public int temp = 0;
    public String[] text ={"1490", "14740", "20940", "27140", "33100", "40880", "46790", "53830", "59680", "66410", "69760", "73000", "79610", "82970", "86260", "93340", "96320", "100340", "106380", "114580", "119970", "123060", "126380", "133320", "140680", "174980", "180190", "187590", "192830", "199590", "203160", "206520", "213210", "216630", "219930", "226040", "230090", "233340", "239900", "248300", "253060", "256620", "259970", "266730", "274640", "281540", "99999999"};// {"�����֣�����sunnyradio", "�ն�D���ж��", " �����ʻ������絽��������", " �������Ĵ�����ô��һ����ȥ", " ����������������氮�޵�", " ������ʵ��һ��ͭǽ���", " Ϊ��һ��Ϳ�������þ�ȫ��", " �����ԫ�ϱ���ֻʣ����ľ���", " ��ס����վ�������������", " �ҽ���ף��������������", " �����ʮ��·��", " �Ҹ�����������", " �ᶨ��ѡ���Ժ��¸ҵ���", " ���»������ѹ�", " ����Ȼ���Լ�˵", " ����һ��Ϊ�˰��¾��ս", " ��ǰ��", " һֱ�����ĺ���", " ��;û����������Ҳ���ᶯҡ", " ѡ��һ����ǰ������ʵ�ж��Ѱ�", " ���޷����һ���", " ������", " ����ͷ��ǿ����", " ���е��Ҹ������һ�����ȼ��", " �����Ѿ�ʧȥҲҪ�¸�ȥ׷��", " ���������·�ѿ", " δ�����ۻ��ж��ٱ�������", " ӭ�ŷ����ҽ���Ȼѡ���氮��ȥ", " ��Ϊ����̫���ջ���������", " �ں�ÿһ��̤�����㼣", " �����ʮ��·��", " �Ҹ�����������", " �����ⲽ����ԥ˦�����", " ����������ѹ�", " ��������ѡ��", " ����һ��Ϊ�˰��¾��ս", " ��ǰ��", " һֱ�����ĺ���", " ��;û����������Ҳ���ᶯҡ", " ѡ��һ����ǰ������ʵ�ж��Ѱ�", " ���޷����һ���", " ������", " ����ͷ��ǿ����", " ���е��Ҹ������һ�����ȼ��", " �����Ѿ�ʧȥҲҪ�¸�ȥ׷��", " ��ȥ����ĵ���", " ѡ��������ҡ"};
    public String[] time = {"1490", "14740", "20940", "27140", "33100", "40880", "46790", "53830", "59680", "66410", "69760", "73000", "79610", "82970", "86260", "93340", "96320", "100340", "106380", "114580", "119970", "123060", "126380", "133320", "140680", "174980", "180190", "187590", "192830", "199590", "203160", "206520", "213210", "216630", "219930", "226040", "230090", "233340", "239900", "248300", "253060", "256620", "259970", "266730", "274640", "281540", "99999999"};

    public LyricView2(Context context) {
        super(context);
        init();
    }

    public LyricView2(Context context, AttributeSet attr) {
        super(context, attr);
        init();
    }

    public LyricView2(Context context, AttributeSet attr, int i) {
        super(context, attr, i);
        init();
    }

    private void init() {
        setFocusable(true);
        //�Ǹ�������
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextSize(18);
        mPaint.setColor(Color.GREEN);
        mPaint.setTypeface(Typeface.SERIF);
        //�������� ��ǰ���
        mPathPaint = new Paint();
        mPathPaint.setAntiAlias(true);
        mPathPaint.setTextSize(21);
        mPathPaint.setColor(Color.RED);
        mPathPaint.setTypeface(Typeface.SANS_SERIF);
    }

    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        //�������
        //��ʾ������
        int j = (int) (-drifty / 40);
        if (temp < j) {
            temp++;
        } else if (temp > j) {
            temp--;
        }

        if (index + temp >= 0 && index + temp < time.length - 1) drift_r = drifty;

        canvas.drawColor(0xEFeffff);
        Paint p = mPaint;
        Paint p2 = mPathPaint;
        p.setTextAlign(Paint.Align.CENTER);

        if (index == -1) return;
        p2.setTextAlign(Paint.Align.CENTER);
        //�Ȼ���ǰ�У�֮���ٻ����ǰ��ͺ��棬����ͱ����˵�ǰ�����м��λ��
        canvas.drawText(text[index], mX, middleY + drift_r, p2);
        if (showprogress && index + temp < time.length - 1) {
            p2.setTextAlign(Paint.Align.LEFT);
            if (index + temp >= 0) {
                canvas.drawText(TimeParseTool.MsecParseTime(time[index + temp]), 0, middleY, p2);
            } else canvas.drawText("00:00", 0, middleY, p2);
            canvas.drawLine(0, middleY + 1, mX * 2, middleY + 1, p2);
        }
        float tempY = middleY + drift_r;
        //��������֮ǰ�ľ���
        for (int i = index - 1; i >= 0; i--) {
            //��������
            tempY = tempY - DY;
            if (tempY < 0) {
                break;
            }
            canvas.drawText(text[i], mX, tempY, p);
        }
        tempY = middleY + drift_r;
        //��������֮��ľ���
        for (int i = index + 1; i < text.length; i++) {
            //��������
            tempY = tempY + DY;
            if (tempY > mY) {
                break;
            }
            canvas.drawText(text[i], mX, tempY, p);
        }


    }

    protected void onSizeChanged(int w, int h, int ow, int oh) {
        super.onSizeChanged(w, h, ow, oh);
        mX = w * 0.5f;//��Ļ�������(ת��Ϊfloat?)
        mY = h;
        middleY = h * 0.5f;
    }

    /**
     * @param CurrentPosition ��ǰ��ʵ�ʱ����
     * @return drift
     * ���Է�����ݣ��Ѿ�����
     * @author younger
     */
    public float updateindex(int CurrentPosition) {
        //�����������
        if (CurrentPosition >= Integer.parseInt(time[index]) && CurrentPosition < Integer.parseInt(time[index + 1])) {
            currentDuringTime = Integer.parseInt(time[index + 1]) - Integer.parseInt(time[index]);
            index++;
            drifty = 0;
            driftx = 0;
        } else if (index == 0) {
            currentDuringTime = Integer.parseInt(time[index]);
        }
        if (drifty > -40.0) drifty = (float) (drifty - 40.0 / (currentDuringTime / 100));

        if (index == -1) return -1;
        return drifty;
    }

    public boolean repair() {
        if (index <= 0) {
            index = 0;
            return false;
        }
        if (index > time.length - 1) index = time.length - 1;
        return true;
    }

}
